package com.ldk.springboot2021.controller;

import com.ldk.springboot2021.pojo.Order;
import com.ldk.springboot2021.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@RestController  返回的所有都是json数据时才用
@Controller
@RequestMapping("/test")
public class ThymeleafController {
    @Autowired
    private OrderService orderService;
    //使用ModelAndView
    @RequestMapping("/t1")
    public ModelAndView t1(ModelAndView modelAndView){
        //封装视图
        modelAndView.setViewName("test1");//视图名
        //封装数据
        modelAndView.addObject("msg","学习Thymeleaf");
        return modelAndView;
    }

    //使用Model对象绑定数据
    @RequestMapping("/t2")
        public String t2(Model model){
        model.addAttribute("count",1000);
        model.addAttribute("name","李四");
        return "test1";//视图名
    }
    //使用HttpServletRequest对象
    @RequestMapping("t3")
    public String t3(HttpServletRequest request){
        request.setAttribute("address","广东茂名");
        return "test1";
    }
    //使用HttpSession 绑定数据
    @RequestMapping("t4")
    public String t4(HttpSession httpSession){
        httpSession.setAttribute("money",10001);
        return "test1";
    }
    //假如使用Model 绑定对象返回
    @RequestMapping("t5")
    public String t5(Model model){
        Order order=new Order().setOrderName("麦当劳").setPrice("1000")
                .setOrderId(10);
        model.addAttribute("o",order);
        return "test1";
    }

    //
    @RequestMapping("t6")
    public String t6(Model model){
    model.addAttribute("msg","123456");
        Date time=new Date();
        System.out.println(time);
        model.addAttribute("time",time);
        model.addAttribute("sex","女");
        return "test";
    }
    @RequestMapping("t7")
    public String t7(Model model){

        List<Order> list=orderService.lookAllOrder();
        model.addAttribute("orderList",list);
        return "test3";
    }
//从网址t8  跳转到  test4
    @RequestMapping("t8")
    public String t8(Model model){

        return "test4";
    }

    @RequestMapping("/save")
    //ResponseBody：将函数以json串的方式返回
    @ResponseBody
    public String save(String content){

        return content;
    }

    @RequestMapping("/update")
    //ResponseBody：将函数以json串的方式返回
    @ResponseBody
    public String update(){
        System.out.println("update请求");
        return "ok";
    }
    @RequestMapping("/delete")
    //ResponseBody：将函数以json串的方式返回
    @ResponseBody
    public String delete(Integer id,String name){
        System.out.println(id);
        System.out.println(name);
        return id+","+name;
    }


}
